const router = require('koa-router')();
const loginService = require('../service/loginService');
const commonUtils = require('../util/commonUtils');
router.prefix('/api/admin');

/**
 * @api {post} /api/admin/login 登录后台
 * @apiName login
 * @apiGroup admin
 *
 * @apiParam {String} username 用户名.
 * @apiParam {String} password 登录密码.
 *
 * @apiSuccess {int} code 返回状态码（1：登录成功，2：登录失败，3：服务器出错，4：参数不全）
 * @apiSuccess {String} msg 返回状态码对应的信息
 * @apiSuccess {String} token 返回token值
 *
 */

router.post('/login', async (ctx) => {
    try {
        let data = ctx.request.body;
        let username = data.username,
            password = data.password;
        if (!username || !password) {
            let data = {code: 4, msg: '参数不全！'};
            ctx.body = data;
            return;
        }
        await loginService.handleLogin(data).then((result) => {
            ctx.body = result;
        }, (err) => {
            ctx.body = err;
        }).catch((error) => {
            ctx.body = error;
        })
    } catch (e) {
        let data = {code: 3, msg: e.message};
        ctx.body = data;
    }
});


/**
 * @api {post} /api/admin/addAccount 添加账户
 * @apiName addAccount
 * @apiGroup admin
 *
 * @apiParam {String} username 用户名.
 * @apiParam {String} password 登录密码.
 * @apiParam {String} token token值.
 *
 * @apiSuccess {int} code 返回状态码（1：添加成功，2：添加失败，3：服务器出错，4：参数不全）
 * @apiSuccess {String} msg 返回状态码对应的信息
 *
 */

router.post('/addAccount', async (ctx) => {
    try {
        const data = ctx.request.body;
        let username = data.username,
            password = data.password;
        if (!username || !password) {
            let data = {code: 4, msg: '参数不全！'};
            ctx.body = data;
            return;
        }
        await loginService.handleAddAccount(data).then((result) => {
            ctx.body = result;
        }, (err) => {
            ctx.body = err;
        }).catch((error) => {
            ctx.body = error;
        })
    } catch (e) {
        let data = {code: 3, msg: e.message};
        ctx.body = data;
    }
});

/**
 * @api {post} /api/admin/editAccount 修改密码
 * @apiName editAccount
 * @apiGroup admin
 *
 * @apiParam {String} username 用户名.
 * @apiParam {String} password 登录密码.
 *
 * @apiSuccess {int} code 返回状态码（1：修改成功，2：修改失败，3：服务器出错，4：参数不全）
 * @apiSuccess {String} msg 返回状态码对应的信息
 *
 */

router.post('/editAccount', async (ctx) => {
    try {
        const data = ctx.request.body;
        let username = data.username,
            password = data.password;
        if (!username || !password) {
            let data = {code: 4, msg: '参数不全！'};
            ctx.body = data;
            return;
        }
        await loginService.handleEditAccount(data).then((result) => {
            ctx.body = result;
        }, (err) => {
            ctx.body = err;
        }).catch((error) => {
            ctx.body = error;
        })
    } catch (e) {
        let data = {code: 3, msg: e.message};
        ctx.body = data;
    }
});

/**
 * @api {post} /api/admin/deleteAccount 删除账户
 * @apiName deleteAccount
 * @apiGroup admin
 *
 * @apiParam {String} username 用户名.
 *
 * @apiSuccess {int} code 返回状态码（1：删除成功，2：删除失败，3：服务器出错，4：参数不全）
 * @apiSuccess {String} msg 返回状态码对应的信息
 *
 */

router.post('/deleteAccount', async (ctx) => {
    try {
        const data = ctx.request.body;
        let username = data.username;
        if (!username) {
            let data = {code: 4, msg: '参数不全！'};
            ctx.body = data;
            return;
        }
        await loginService.handleDeleteAccount(data).then((result) => {
            ctx.body = result;
        }, (err) => {
            ctx.body = err;
        }).catch((error) => {
            ctx.body = error;
        })
    } catch (e) {
        let data = {code: 3, msg: e.message};
        ctx.body = data;
    }
});

/**
 * @api {post} /api/admin/getAccountList 获取账户列表
 * @apiName getAccountList
 * @apiGroup admin
 *
 * @apiSuccess {int} code 返回状态码（1：获取成功，2：获取失败，3：服务器出错，4：参数不全）
 * @apiSuccess {String} msg 返回状态码对应的信息
 * @apiSuccess {String} allSize 数据总数
 * @apiSuccess {Object[]} data 获取成功时返回的数据
 * @apiSuccess {String} data.number 序号
 * @apiSuccess {String} data.username 用户名
 * @apiSuccess {String} data.created_time 创建时间
 *
 */

router.post('/getAccountList', async (ctx) => {
    try {
        await loginService.handleGetAccountList().then((result) => {
            ctx.body = result;
        }, (err) => {
            ctx.body = err;
        }).catch((error) => {
            ctx.body = error;
        })
    } catch (e) {
        let data = {code: 3, msg: e.message};
        ctx.body = data;
    }
});



module.exports = router;